{{define "title"}}
  {{ with .Build.Summary -}}
    {{ if eq .Status.String "InfraFailure" }}
      Infra Failure
    {{ else if eq .Status.String "Exception" }}
      Exception
    {{ else if eq .Status.String "Failure" }}
      Failed
    {{ else if eq .Status.String "NotRun" }}
      Pending
    {{ else }}
      {{ .Status.String }}
    {{ end }}
    -
    {{ .ParentLabel }} {{ .Label }}
  {{- end }}
{{end}}

{{define "head"}}
<script>
  const useTabs = false;
</script>
<script src="/static/common/js/build.js"></script>
<link rel="stylesheet" href="/static/common/css/tabs.css" type="text/css">
{{end}}

{{define "interval"}}
  {{ if .Started }}
    <span class="duration"
          data-starttime="{{ .Started | formatTime }}"
          {{ if not .Finished.IsZero -}}
            data-endtime="{{ .Finished | formatTime }}"
          {{- end }}>
          ( {{ .Duration | humanDuration }} )</span>
  {{ end }}
{{end}}

{{define "favicon"}}
<link id="favicon" rel="shortcut icon" type="image/png" href="/static/common/favicon/
{{- with .Build.Summary.Status.String -}}
  {{- if eq . "Running" -}} yellow
  {{- else if eq . "Success" -}} green
  {{- else if eq . "InfraFailure" -}} purple
  {{- else if eq . "Exception" -}} purple
  {{- else if eq . "Expired" -}} darkpurple
  {{- else if eq . "Failure" -}} red
  {{- else if eq . "Canceled" -}} red
  {{- else if eq . "NotRun" -}} gray
  {{- else if eq . "Warning" -}} yellow
  {{- else if eq . "DependencyFailure" -}} redamber
  {{- else if eq . "WaitingDependency" -}} brown
  {{- else -}} milo
  {{- end -}}
{{- end -}}-32.png">

{{end}}

{{define "component"}}
<li class="{{ if eq .Status.String "Success" }}green{{ end }}
           {{- if .Children }} substeps
             {{- if .Collapsed }} collapsed{{ end }}
           {{- end }}">
  <div class="status-{{.Status}} result">
      {{ template "interval" .ExecutionTime }}
    <b>{{.Label.HTML}}</b>
    <span>
      {{ range .TextBR -}}
        <div class="step-text">
          {{- if . -}}{{- . -}}{{- else -}}&nbsp;{{- end -}}
        </div>
      {{- end }}
    </span>
  </div>
  <ul>
    {{ if .MainLink }}
      <li class="sublink">{{.MainLink.HTML}}</li>
      {{- if ne .Status.String "Success" -}}
      {{ end }}
    {{- end }}
    {{ range .SubLink }}
      <li class="sublink {{- if .IsDebugLink }} debug-log{{end}}">{{.HTML}}</li>
    {{ end }}
    {{ if not (or .MainLink .SubLink) }}
      <li class="sublink"> - no logs - </li>
    {{ end }}
  </ul>
  {{ if .Children }}
    <ol>
    {{ range .Children  }}
      {{ template "component" . }}
    {{ end }}
    </ol>
  {{ end }}
</li>
{{ end }}

{{define "body"}}
  <div class="content">
    <h1>
    {{ if .Build.Summary.ParentLabel }}
      Builder {{ .Build.Summary.ParentLabel.HTML }}
    {{ end }}
    Build {{.Build.Summary.Label.HTML }}
    {{ if .Build.Summary.Banner }}
      {{ range .Build.Summary.Banner.OS }}
        <img src="/static/common/logos/{{.LogoBase.Img}}" alt="{{.LogoBase.Alt}}"
             width="25px">
      {{ end }}
      {{ range .Build.Summary.Banner.Device }}
        <img src="/static/common/logos/{{.LogoBase.Img}}" alt="{{.LogoBase.Alt}}"
             width="25px">
      {{ end }}
    {{ end }}
    </h1>

    <div id="tabs" style="display: none;">
      <ul>
        <li><a href="#overview-tab">Overview</a></li>
      </ul>
      <div id="overview-tab">
        {{ template "overview" . }}
        {{ template "properties" . }}
        {{ template "changes" . }}
      </div>
    </div>
  </div>
{{end}}

{{define "overview"}}
  <div id="results" class="column">
    <h2>Results:</h2>
    {{ with .Build.Summary }}
      <p class="result status-{{.Status}}">
      {{ if .Text }}{{ range .TextBR }}<span class="step-text">{{ . }} </span>{{ end }}
      {{ else if eq .Status.String "InfraFailure" }}
        Internal Failure
      {{ else if eq .Status.String "Expired" }}
        Internal Failure - Expired
      {{ else if eq .Status.String "NotRun" }}
        Pending
      {{ else }}
        {{.Status}}
      {{ end }}
      </p>
    {{ end }} <!-- with -->

    {{ if .Build.Trigger }}
    {{ with .Build.Trigger }}

    <h2>Trigger Info:</h2>

    <table class="info" width="100%">
    {{ if .Project }}
      <tr><td class="left">Project</td><td>{{ .Project }}</td></tr>
    {{ end }}

    {{ if .Commit.Repo }}
      <tr><td class="left">Repository</td><td>{{ .Commit.Repo }}</td></tr>
    {{ end }}

    {{ if .Commit.Branch }}
      <tr><td class="left">Branch</td><td>{{ .Commit.Branch }}</td></tr>
    {{ end }}

    {{ if .Commit.RequestRevision }}
      <tr><td class="left">Revision</td><td>{{ .Commit.RequestRevision.HTML }}</td></tr>
    {{ end }}

    {{ if .Commit.Revision }}
      <tr><td class="left">Got Revision</td><td>{{ .Commit.Revision.HTML }}</td></tr>
    {{ end }}

    {{ if .Commit.Changelist }}
      <tr><td class="left">Patch</td><td>{{ .Commit.Changelist.HTML }}</td></tr>
    {{ end }}

    </table>
    {{ end }}  <!-- with -->
    {{ end }}  <!-- if -->

    {{ if or .Build.Summary.Bot .Build.Summary.Source }}
    <h2>Execution:</h2>
    <ul>
      {{ if .Build.Summary.Source }}
      <li>
        Source:
        <a href="{{ .Build.Summary.Source.URL }}"
           aria-label="Source for {{ .Build.Summary.Source.Label }}">
          {{ .Build.Summary.Source.Label }}
        </a>
      </li>
      {{ end }}

      {{ if .Build.Summary.Bot }}
      <li>
        Bot:
        <a href="{{ .Build.Summary.Bot.URL }}"
           aria-label="Bot {{ .Build.Summary.Bot.Label }}">
          {{ .Build.Summary.Bot.Label }}
        </a>
      </li>
      {{ end }}

      {{ if .Build.Summary.Recipe }}
      <li>
        Recipe: {{ .Build.Summary.Recipe.HTML }}
      </li>
      {{ end }}
    </ul>
    {{ end }} <!-- if -->

    {{ if .Build.Trigger }}
      {{ if .Build.Trigger.Source }}
        <h2>Reason:</h2>
        <p>{{ .Source }}</p>
      {{ end }}
    {{ end }}

    {{ if or .Build.Components .Build.Summary.SubLink }}
      <h2>Steps and Logfiles:</h2>
      Show:
      <input type="radio" name="hider" id="showExpanded" value="expanded"
             {{- if eq .Build.StepDisplayPref "expanded" }} checked{{ end }}>
      <label for="showExpanded">Expanded</label>
      <input type="radio" name="hider" id="showDefault" value="default"
             {{- if eq .Build.StepDisplayPref "default" }} checked{{ end }}>
      <label for="showDefault">Default</label>
      <input type="radio" name="hider" id="showNonGreen" value="non-green"
             {{- if eq .Build.StepDisplayPref "non-green" }} checked{{ end }}>
      <label for="showNonGreen">Non-Green</label>

      <br/>
      Debug Logs:
      <input type="checkbox" name="debug_hider" id="showDebugLogs"
             {{- if .Build.ShowDebugLogsPref }} checked{{ end }}>

      <ol id="steps" class="
        {{- if eq .Build.StepDisplayPref "non-green" }} non-green{{ end -}}
        {{- if not .Build.ShowDebugLogsPref }} hide-debug-logs{{ end -}}
        ">

        {{ with .Build.Summary }}
          {{ if .SubLink }}
          <li>
            <div class="status-{{.Status}} result">
              {{ template "interval" .ExecutionTime }}
              <b>Steps</b>
            </div>
            <ul>
              {{ range .SubLink  }}
              <li class="sublink">{{ .HTML }}</li>
              {{ end }}
            </ul>
          </li>
          {{ end }}
        {{ end }}

        {{ range .Build.Components }}
          {{ template "component" . }}
        {{ end }}
      </ol>
    {{ end }} <!-- if .Build.Components -->

  </div>
{{end}}

{{define "properties"}}
  <div id="properties" class="column">
    <h2>Build Properties:</h2>

    <table class="info BuildProperties" width="100%">
    <tr><th>Name</th><th>Value</th><th>Source</th></tr>

      {{ range .Build.PropertyGroup }}
      {{ $name := .GroupName }}
        {{ range .Property }}
          <tr>
            <td class="left">{{.Key}}</td>
            <td class="middle"><abbr title="{{.Value}}">{{.Value}}</abbr></td>
            <td>{{$name}}</td>
          </tr>
        {{ end }}
      {{ end }}
    </table>

    {{ if .Build.Blame }}
      <h2>Blamelist:</h2>
      <ol>
      {{ range .Build.Blame }}
        <li>{{ .AuthorName }} ({{ .AuthorEmail | obfuscateEmail }})</li>
      {{ end }}
      </ol>
    {{ end }}

    <h2>Timing:</h2>
    <table class="info" width="100%">
      <tr class="alt"><td class="left">Create</td>
      <td>
        {{ .Build.Summary.PendingTime.Started | localTime "N/A" }}
      </td></tr>
      <tr><td class="left">Start</td>
      <td>
        {{ .Build.Summary.ExecutionTime.Started | localTime "N/A" }}
      </td></tr>
      <tr class="alt"><td class="left">End</td>
      <td>
        {{ .Build.Summary.ExecutionTime.Finished | localTime "N/A" }}
      </td></tr>
      <tr><td class="left">Pending</td>
      <td id="duration">
        {{ if .Build.Summary.PendingTime.Started.IsZero }}
          N/A
        {{ else }}
          {{ .Build.Summary.PendingTime.Duration | humanDuration }}
        {{ end }}
      </td></tr>
      <tr class="alt"><td class="left">Execution</td>
      <td id="duration">
        {{ if .Build.Summary.ExecutionTime.Started.IsZero }}
          N/A
        {{ else }}
          {{ .Build.Summary.ExecutionTime.Duration | humanDuration }}
        {{ end }}
      </td></tr>
    </table>

  </div>
{{end}}

{{define "changes"}}
  <div id="changes" class="column">
    {{ if .Build.Blame }}
    <h2>All Changes:</h2>
    <ol>
    {{ range .Build.Blame }}
    <li>
      <h3>{{.Title}}</h3>
      <table class="info">
        <tbody>
          <tr>
            <td class="left">Changed by</td>
            <td class="value">
                {{ if .AuthorName }}{{ .AuthorName }} - {{ end }}
                {{ .AuthorEmail | obfuscateEmail }}
            </td>
          </tr>
          <tr>
            <td class="left">Changed at</td>
            <td class="value">{{ .CommitTime | localTime "N/A" }}</td>
          </tr>
          <tr>
            <td class="left">Repository</td>
            <td class="value">{{ .Repo }}</td>
          </tr>
          <tr>
            <td class="left">Branch</td>
            <td class="value">{{ .Branch }}</td>
          </tr>
          {{ with .Revision }}
            <tr>
              <td class="left">Revision</td>
              <td class="value">{{ .HTML }}</td>
            </tr>
          {{ end }}
        </tbody>
      </table>

      {{ if .Description }}
        <h3>Comments</h3>
        <pre class="comments">{{ .Description | formatCommitDesc }}</pre>
      {{ end }}

      {{ if .File }}
        <h3 class="files">Changed files</h3>
        <ul class="alternating">
          {{ range .File }}
          <li class="file">{{ . }}</li>
          {{ end }}
        </ul>
      {{ end }}

    </li>
    {{ end }}
    </ol>
    {{ end }}
  </div>
{{end}}
